package com.hig.questions.service.impl;

import java.util.*;

import cn.hutool.core.collection.CollectionUtil;
import com.hig.questions.domain.ExamQuestionsAnswer;
import com.hig.questions.domain.ExamQuestionsBank;
import com.hig.questions.domain.ExamQuestionsContent;
import com.hig.questions.excel.ExamQuestionsPropertyExcel;
import com.hig.questions.mapper.ExamQuestionsAnswerMapper;
import com.hig.questions.mapper.ExamQuestionsBankMapper;
import com.hig.questions.mapper.ExamQuestionsContentMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hig.questions.mapper.ExamQuestionsPropertyMapper;
import com.hig.questions.domain.ExamQuestionsProperty;
import com.hig.questions.service.IExamQuestionsPropertyService;
import org.springframework.util.ObjectUtils;

/**
 * 题目属性Service业务层处理
 *
 * @author qnsdt
 * @date 2022-12-28
 */
@Service
@Slf4j
public class ExamQuestionsPropertyServiceImpl implements IExamQuestionsPropertyService {
    @Autowired
    private ExamQuestionsPropertyMapper examQuestionsPropertyMapper;

    @Autowired
    private ExamQuestionsAnswerMapper answerMapper;
    private static final Map<String, Integer> questionTypeMap = new HashMap<>();

    static {
        questionTypeMap.put("判断题", 1);
        questionTypeMap.put("单选题", 2);
        questionTypeMap.put("多选题", 3);
    }

    /**
     * 查询题目属性
     *
     * @param questionsCode 题目属性ID
     * @return 题目属性
     */
    @Override
    public ExamQuestionsProperty selectExamQuestionsPropertyById(String questionsCode) {
        return examQuestionsPropertyMapper.selectExamQuestionsPropertyById(questionsCode);
    }

    /**
     * 查询题目属性列表
     *
     * @param examQuestionsProperty 题目属性
     * @return 题目属性
     */
    @Override
    public List<ExamQuestionsProperty> selectExamQuestionsPropertyList(ExamQuestionsProperty examQuestionsProperty) {
        return examQuestionsPropertyMapper.selectExamQuestionsPropertyList(examQuestionsProperty);
    }

    /**
     * 新增题目属性
     *
     * @param examQuestionsProperty 题目属性
     * @return 结果
     */
    @Override
    public int insertExamQuestionsProperty(ExamQuestionsProperty examQuestionsProperty) {
        examQuestionsProperty.setCreateTime(DateUtils.getNowDate());
        Long nextQuestionsId = examQuestionsPropertyMapper.getNextQuestionsId();
        examQuestionsProperty.setMaxQuestionId(nextQuestionsId);
        return examQuestionsPropertyMapper.insertExamQuestionsProperty(examQuestionsProperty);
    }

    /**
     * 修改题目属性
     *
     * @param examQuestionsProperty 题目属性
     * @return 结果
     */
    @Override
    public int updateExamQuestionsProperty(ExamQuestionsProperty examQuestionsProperty) {
        return examQuestionsPropertyMapper.updateExamQuestionsProperty(examQuestionsProperty);
    }

    /**
     * 批量删除题目属性
     *
     * @param questionsCodes 需要删除的题目属性ID
     * @return 结果
     */
    @Override
    public int deleteExamQuestionsPropertyByIds(String[] questionsCodes) {
        return examQuestionsPropertyMapper.deleteExamQuestionsPropertyByIds(questionsCodes);
    }

    /**
     * 删除题目属性信息
     *
     * @param questionsCode 题目属性ID
     * @return 结果
     */
    @Override
    public int deleteExamQuestionsPropertyById(String questionsCode) {
        return examQuestionsPropertyMapper.deleteExamQuestionsPropertyById(questionsCode);
    }

    @Override
    public void updateExamQuestionsPropertyStatus(String questionsCode) {
        examQuestionsPropertyMapper.updateExamQuestionsPropertyStatus(questionsCode);
    }

    public static Integer getValueByLabel(String label) {
        return questionTypeMap.get(label);
    }

    @Autowired
    private ExamQuestionsContentMapper contentMapper;

    @Autowired
    private ExamQuestionsBankMapper bankMapper;



    @Override
    public String importQuestions(List<ExamQuestionsPropertyExcel> propertyList, boolean updateSupport, String operName, String bankeCode) {

        if (StringUtils.isNull(propertyList) || propertyList.size() == 0) {
            throw new CustomException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ExamQuestionsPropertyExcel property : propertyList) {
            String questionsType = property.getQuestionsType();
            String rightAnswer = property.getRightAnswer();
            Integer valueByLabel = getValueByLabel(questionsType);
            if (ObjectUtils.isEmpty(valueByLabel)) {
                failureNum++;
                continue;

            }
            HashSet<String> bankCodes = new HashSet<>();
            bankCodes.add(bankeCode);
            String accord = property.getAccord();
            if (StringUtils.isNotEmpty(accord)){
                String[] split = accord.split(",");
                if (split.length > 0) {
                    for (String s : split) {
                        ExamQuestionsBank examQuestionsBank = new ExamQuestionsBank();
                        examQuestionsBank.setBankName(s);
                        List<ExamQuestionsBank> examQuestionsBanks = bankMapper.selectExamQuestionsBankListByName(examQuestionsBank);
                        if (CollectionUtil.isNotEmpty(examQuestionsBanks)) {
                            ExamQuestionsBank eb = examQuestionsBanks.get(0);
                            bankCodes.add(eb.getBankCode());
                        }

                    }
                }

            }
            for (String bCode : bankCodes) {
                try {
                    // 验证是否存在这个用户
                    ExamQuestionsProperty queryProperty = examQuestionsPropertyMapper.selectExamQuestionsPropertyByTitle(property.getQuestionsTitle(),bCode);

                    if (queryProperty==null) {

                        String questionsCode = UUID.randomUUID().toString();
                        // 先新建题干
                        ExamQuestionsContent examQuestionsContent = new ExamQuestionsContent(questionsCode, property.getQuestionsTitle());

                        contentMapper.insertExamQuestionsContent(examQuestionsContent);
                        queryProperty = new ExamQuestionsProperty();
                        queryProperty.setBankCode(bCode);
                        queryProperty.setQuestionsCode(questionsCode);
                        queryProperty.setQuestionsTitle(property.getQuestionsTitle());
                        queryProperty.setQuestionsScore(2);
                        if (valueByLabel == 1) {
                            String optionA = property.getOptionA();
                            String optionB = property.getOptionB();
                            String rightAnswerPanduan = property.getRightAnswer();
                            if (StringUtils.isNotEmpty(rightAnswerPanduan)&&rightAnswerPanduan.equals(optionA)) {
                                queryProperty.setRightAnswer("A");
                            }
                            if (StringUtils.isNotEmpty(rightAnswerPanduan)&&rightAnswerPanduan.equals(optionB)) {
                                queryProperty.setRightAnswer("B");
                            }

                        }else {
                            queryProperty.setRightAnswer(String.join(",", rightAnswer.split("")));
                        }

                        Integer intRate = 1;
                        String rateNumber = property.getRateNumber();
                        if ("适中".equals(rateNumber)) {
                            intRate = 3;
                        }
                        queryProperty.setRateNumber(intRate);

                        queryProperty.setQuestionsType(valueByLabel);
                        String optionA = property.getOptionA();
                        String optionB = property.getOptionB();
                        String optionC = property.getOptionC();
                        String optionD = property.getOptionD();
                        String optionE = property.getOptionE();
                        String optionF = property.getOptionF();
                        String optionG = property.getOptionG();
                        String optionH = property.getOptionH();
                        if (StringUtils.isNotEmpty(optionA)) {
                            ExamQuestionsAnswer examQuestionsAnswer = new ExamQuestionsAnswer();
                            examQuestionsAnswer.setQuestionsCode(questionsCode);
                            examQuestionsAnswer.setOrderId(1L);

                            if (valueByLabel==1){
                                examQuestionsAnswer.setIsRight(queryProperty.getRightAnswer().equals("A") ? 1 : 0);
                            }else {
                                examQuestionsAnswer.setIsRight(valueByLabel == 3 ? 0 : (rightAnswer.contains("A") ? 1 : 0));
                            }

                            examQuestionsAnswer.setOptionCode("A");
                            examQuestionsAnswer.setOptionDescribe(optionA);
                            answerMapper.insertExamQuestionsAnswer(examQuestionsAnswer);
                        }
                        if (StringUtils.isNotEmpty(optionB)) {
                            ExamQuestionsAnswer examQuestionsAnswer = new ExamQuestionsAnswer();
                            examQuestionsAnswer.setQuestionsCode(questionsCode);
                            examQuestionsAnswer.setOrderId(2L);
                            if (valueByLabel==1){
                                examQuestionsAnswer.setIsRight(queryProperty.getRightAnswer().equals("B") ? 1 : 0);
                            }else {
                                examQuestionsAnswer.setIsRight(valueByLabel == 3 ? 0 : (rightAnswer.contains("B") ? 1 : 0));
                            }

                            examQuestionsAnswer.setOptionCode("B");
                            examQuestionsAnswer.setOptionDescribe(optionB);
                            answerMapper.insertExamQuestionsAnswer(examQuestionsAnswer);
                        }
                        if (StringUtils.isNotEmpty(optionC)) {
                            ExamQuestionsAnswer examQuestionsAnswer = new ExamQuestionsAnswer();
                            examQuestionsAnswer.setQuestionsCode(questionsCode);
                            examQuestionsAnswer.setOrderId(3L);
                            examQuestionsAnswer.setIsRight(valueByLabel == 3 ? 0 : (rightAnswer.contains("C") ? 1 : 0));
                            examQuestionsAnswer.setOptionCode("C");
                            examQuestionsAnswer.setOptionDescribe(optionC);
                            answerMapper.insertExamQuestionsAnswer(examQuestionsAnswer);
                        }
                        if (StringUtils.isNotEmpty(optionD)) {
                            ExamQuestionsAnswer examQuestionsAnswer = new ExamQuestionsAnswer();
                            examQuestionsAnswer.setQuestionsCode(questionsCode);
                            examQuestionsAnswer.setOrderId(4L);
                            examQuestionsAnswer.setIsRight(valueByLabel == 3 ? 0 : (rightAnswer.contains("D") ? 1 : 0));
                            examQuestionsAnswer.setOptionCode("D");
                            examQuestionsAnswer.setOptionDescribe(optionD);
                            answerMapper.insertExamQuestionsAnswer(examQuestionsAnswer);
                        }
                        if (StringUtils.isNotEmpty(optionE)) {
                            ExamQuestionsAnswer examQuestionsAnswer = new ExamQuestionsAnswer();
                            examQuestionsAnswer.setQuestionsCode(questionsCode);
                            examQuestionsAnswer.setOrderId(5L);
                            examQuestionsAnswer.setIsRight(valueByLabel == 3 ? 0 : (rightAnswer.contains("E") ? 1 : 0));
                            examQuestionsAnswer.setOptionCode("E");
                            examQuestionsAnswer.setOptionDescribe(optionE);
                            answerMapper.insertExamQuestionsAnswer(examQuestionsAnswer);
                        }
                        if (StringUtils.isNotEmpty(optionF)) {
                            ExamQuestionsAnswer examQuestionsAnswer = new ExamQuestionsAnswer();
                            examQuestionsAnswer.setQuestionsCode(questionsCode);
                            examQuestionsAnswer.setOrderId(6L);
                            examQuestionsAnswer.setIsRight(valueByLabel == 3 ? 0 : (rightAnswer.contains("F") ? 1 : 0));
                            examQuestionsAnswer.setOptionCode("F");
                            examQuestionsAnswer.setOptionDescribe(optionF);
                            answerMapper.insertExamQuestionsAnswer(examQuestionsAnswer);
                        }
                        if (StringUtils.isNotEmpty(optionG)) {
                            ExamQuestionsAnswer examQuestionsAnswer = new ExamQuestionsAnswer();
                            examQuestionsAnswer.setQuestionsCode(questionsCode);
                            examQuestionsAnswer.setOrderId(7L);
                            examQuestionsAnswer.setIsRight(valueByLabel == 3 ? 0 : (rightAnswer.contains("G") ? 1 : 0));
                            examQuestionsAnswer.setOptionCode("G");
                            examQuestionsAnswer.setOptionDescribe(optionG);
                            answerMapper.insertExamQuestionsAnswer(examQuestionsAnswer);
                        }
                        if (StringUtils.isNotEmpty(optionH)) {
                            ExamQuestionsAnswer examQuestionsAnswer = new ExamQuestionsAnswer();
                            examQuestionsAnswer.setQuestionsCode(questionsCode);
                            examQuestionsAnswer.setOrderId(8L);
                            examQuestionsAnswer.setIsRight(valueByLabel == 3 ? 0 : (rightAnswer.contains("H") ? 1 : 0));
                            examQuestionsAnswer.setOptionCode("H");
                            examQuestionsAnswer.setOptionDescribe(optionH);
                            answerMapper.insertExamQuestionsAnswer(examQuestionsAnswer);
                        }

                        this.insertExamQuestionsProperty(queryProperty);
                        successNum++;
//                    successMsg.append("<br/>" + successNum + "、试题 " + queryProperty.getQuestionsTitle() + " 导入成功");
                    } else if (updateSupport) {
//更新
                        String questionsCode = queryProperty.getQuestionsCode();
                        //String questionsCode = UUID.randomUUID().toString();
                        // 先新建题干
                        ExamQuestionsContent examQuestionsContent = new ExamQuestionsContent(questionsCode, property.getQuestionsTitle());

                        contentMapper.updateExamQuestionsContent(examQuestionsContent);
                        queryProperty = new ExamQuestionsProperty();
                        queryProperty.setBankCode(bankeCode);
                        queryProperty.setQuestionsCode(questionsCode);
                        queryProperty.setQuestionsTitle(property.getQuestionsTitle());
                        queryProperty.setQuestionsScore(2);
                        if (valueByLabel == 1) {
                            String optionA = property.getOptionA();
                            String optionB = property.getOptionB();
                            String rightAnswerPanduan = property.getRightAnswer();
                            if (StringUtils.isNotEmpty(rightAnswerPanduan)&&rightAnswerPanduan.equals(optionA)) {
                                queryProperty.setRightAnswer("A");
                            }
                            if (StringUtils.isNotEmpty(rightAnswerPanduan)&&rightAnswerPanduan.equals(optionB)) {
                                queryProperty.setRightAnswer("B");
                            }

                        }else {
                            queryProperty.setRightAnswer(String.join(",", rightAnswer.split("")));
                        }

                        Integer intRate = 1;
                        String rateNumber = property.getRateNumber();
                        if ("适中".equals(rateNumber)) {
                            intRate = 3;
                        }
                        queryProperty.setRateNumber(intRate);

                        queryProperty.setQuestionsType(valueByLabel);
                        String optionA = property.getOptionA();
                        String optionB = property.getOptionB();
                        String optionC = property.getOptionC();
                        String optionD = property.getOptionD();
                        String optionE = property.getOptionE();
                        String optionF = property.getOptionF();
                        String optionG = property.getOptionG();
                        String optionH = property.getOptionH();
                        if (StringUtils.isNotEmpty(optionA)) {
                            ExamQuestionsAnswer examQuestionsAnswer = new ExamQuestionsAnswer();
                            examQuestionsAnswer.setQuestionsCode(questionsCode);
                            examQuestionsAnswer.setOrderId(1L);

                            if (valueByLabel==1){
                                examQuestionsAnswer.setIsRight(queryProperty.getRightAnswer().equals("A") ? 1 : 0);
                            }else {
                                examQuestionsAnswer.setIsRight(valueByLabel == 3 ? 0 : (rightAnswer.contains("A") ? 1 : 0));
                            }

                            examQuestionsAnswer.setOptionCode("A");
                            examQuestionsAnswer.setOptionDescribe(optionA);
                            answerMapper.updateExamQuestionsAnswerByOption(examQuestionsAnswer);
                        }
                        if (StringUtils.isNotEmpty(optionB)) {
                            ExamQuestionsAnswer examQuestionsAnswer = new ExamQuestionsAnswer();
                            examQuestionsAnswer.setQuestionsCode(questionsCode);
                            examQuestionsAnswer.setOrderId(2L);
                            if (valueByLabel==1){
                                examQuestionsAnswer.setIsRight(queryProperty.getRightAnswer().equals("B") ? 1 : 0);
                            }else {
                                examQuestionsAnswer.setIsRight(valueByLabel == 3 ? 0 : (rightAnswer.contains("B") ? 1 : 0));
                            }

                            examQuestionsAnswer.setOptionCode("B");
                            examQuestionsAnswer.setOptionDescribe(optionB);
                            answerMapper.updateExamQuestionsAnswerByOption(examQuestionsAnswer);
                        }
                        if (StringUtils.isNotEmpty(optionC)) {
                            ExamQuestionsAnswer examQuestionsAnswer = new ExamQuestionsAnswer();
                            examQuestionsAnswer.setQuestionsCode(questionsCode);
                            examQuestionsAnswer.setOrderId(3L);
                            examQuestionsAnswer.setIsRight(valueByLabel == 3 ? 0 : (rightAnswer.contains("C") ? 1 : 0));
                            examQuestionsAnswer.setOptionCode("C");
                            examQuestionsAnswer.setOptionDescribe(optionC);
                            answerMapper.updateExamQuestionsAnswerByOption(examQuestionsAnswer);
                        }
                        if (StringUtils.isNotEmpty(optionD)) {
                            ExamQuestionsAnswer examQuestionsAnswer = new ExamQuestionsAnswer();
                            examQuestionsAnswer.setQuestionsCode(questionsCode);
                            examQuestionsAnswer.setOrderId(4L);
                            examQuestionsAnswer.setIsRight(valueByLabel == 3 ? 0 : (rightAnswer.contains("D") ? 1 : 0));
                            examQuestionsAnswer.setOptionCode("D");
                            examQuestionsAnswer.setOptionDescribe(optionD);
                            answerMapper.updateExamQuestionsAnswerByOption(examQuestionsAnswer);
                        }
                        if (StringUtils.isNotEmpty(optionE)) {
                            ExamQuestionsAnswer examQuestionsAnswer = new ExamQuestionsAnswer();
                            examQuestionsAnswer.setQuestionsCode(questionsCode);
                            examQuestionsAnswer.setOrderId(5L);
                            examQuestionsAnswer.setIsRight(valueByLabel == 3 ? 0 : (rightAnswer.contains("E") ? 1 : 0));
                            examQuestionsAnswer.setOptionCode("E");
                            examQuestionsAnswer.setOptionDescribe(optionE);
                            answerMapper.updateExamQuestionsAnswerByOption(examQuestionsAnswer);
                        }
                        if (StringUtils.isNotEmpty(optionF)) {
                            ExamQuestionsAnswer examQuestionsAnswer = new ExamQuestionsAnswer();
                            examQuestionsAnswer.setQuestionsCode(questionsCode);
                            examQuestionsAnswer.setOrderId(6L);
                            examQuestionsAnswer.setIsRight(valueByLabel == 3 ? 0 : (rightAnswer.contains("F") ? 1 : 0));
                            examQuestionsAnswer.setOptionCode("F");
                            examQuestionsAnswer.setOptionDescribe(optionF);
                            answerMapper.updateExamQuestionsAnswerByOption(examQuestionsAnswer);
                        }
                        if (StringUtils.isNotEmpty(optionG)) {
                            ExamQuestionsAnswer examQuestionsAnswer = new ExamQuestionsAnswer();
                            examQuestionsAnswer.setQuestionsCode(questionsCode);
                            examQuestionsAnswer.setOrderId(7L);
                            examQuestionsAnswer.setIsRight(valueByLabel == 3 ? 0 : (rightAnswer.contains("G") ? 1 : 0));
                            examQuestionsAnswer.setOptionCode("G");
                            examQuestionsAnswer.setOptionDescribe(optionG);
                            answerMapper.updateExamQuestionsAnswerByOption(examQuestionsAnswer);
                        }
                        if (StringUtils.isNotEmpty(optionH)) {
                            ExamQuestionsAnswer examQuestionsAnswer = new ExamQuestionsAnswer();
                            examQuestionsAnswer.setQuestionsCode(questionsCode);
                            examQuestionsAnswer.setOrderId(8L);
                            examQuestionsAnswer.setIsRight(valueByLabel == 3 ? 0 : (rightAnswer.contains("H") ? 1 : 0));
                            examQuestionsAnswer.setOptionCode("H");
                            examQuestionsAnswer.setOptionDescribe(optionH);
                            answerMapper.updateExamQuestionsAnswerByOption(examQuestionsAnswer);
                        }

                        this.updateExamQuestionsProperty(queryProperty);
                        successNum++;



                    } else {
                        failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在");
                    }
                } catch (Exception e) {
                    failureNum++;
//                String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败：";
//                failureMsg.append(msg + e.getMessage());
//                log.error(msg, e);
                }
            }

        }
        if (failureNum > 0) {
            successMsg.insert(0, "数据部分导入成功！导入"+successNum+"条,另外有 " + failureNum + " 未导入成功。");
//            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条。");
        }
        return successMsg.toString();
    }
}
